/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Pe.edu.unmsm.SIASS.DAOImpl;

import Pe.edu.unmsm.SIASS.DAO.ProfesorDAO;
import Pe.edu.unmsm.SIASS.DAO.UsuarioDAO;
import Pe.edu.unmsm.SIASS.Dominio.Profesor;
import Pe.edu.unmsm.SIASS.Dominio.Usuario;
import Util.HibernateUtil;
import java.io.Serializable;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.*;

/**
 *
 * @author PC-14
 */
public class ProfesorDAOImpl implements ProfesorDAO, Serializable {

    static Profesor result;

    @Override
    public Profesor obtenerProfesor(int idPersona) {
        Transaction tx = null;
        try {
            Session session = HibernateUtil.getSessionFactory().openSession();
            tx = session.beginTransaction();

            Query query = session.createQuery("FROM Profesor WHERE persona.idPersona="+idPersona);
            if (query.uniqueResult() != null) {
                result = (Profesor) query.uniqueResult();
                Hibernate.initialize(result.getPersona());
                session.beginTransaction().commit();
            } else {
                result = null;
            }
            session.close();
        } catch (HibernateException e) {
            if (tx != null) {
                tx.rollback();
            }
            System.out.println("Error: " + e.getMessage());
        }
        System.out.println(result);
        return result;
    }
}
